package com.cn.wanxi.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.cn.wanxi.dao.CompanyDao;
import com.cn.wanxi.dao.impl.CompanyImpl;
import com.cn.wanxi.model.CompanyModel;
import com.cn.wanxi.service.CompanyService;
import redis.clients.jedis.Jedis;

/**
 * 第四阶段内容
 */
public class CompanyServiceImpl implements CompanyService {
    /**
     * 1.判断缓存里面是否有数据
     * 2.如果有，则直接取缓存数据
     * 3.如果没有，则去数据库数据
     * 4.将数据库数据存入缓存
     *
     * @return
     */
    @Override
    public CompanyModel getCompanyModel() {
        final CompanyModel[] companyModel = {new CompanyModel()};//引用传递
        for (int i = 0; i < 500; i++) {
//            匿名内部类
            int finalI = i;
            new Thread(new Runnable() {
                @Override
                public void run() {
                    int a = 0;
                    a = finalI;
                    companyModel[0] = setRedisData(a);
                }
            }).start();
        }
        return companyModel[0];
    }

    private synchronized CompanyModel setRedisData(int a) {
        CompanyModel companyModel = new CompanyModel();
        Jedis jedis = new Jedis();
        boolean isHave = jedis.exists("logo");
        if (isHave) {
//            String a=jedis.get("companyModel");
//            JSONObject jsonObject=new JSONObject();
//            jsonObject.put("companyModel",companyModel.toString());
//            companyModel=(CompanyModel) jsonObject.get("companyModel");
//            Redis<String ,String>
//            Redis<Object ,Object>
//            Redis<String ,Object>
            companyModel.setLogo(jedis.get("logo"));
            companyModel.setPhone(jedis.get("phone"));
            companyModel.setNet(jedis.get("net"));
            companyModel.setEmail(jedis.get("email"));
            companyModel.setAddress(jedis.get("address"));
            System.out.println("reids" + a);
        } else {
//            下载redis的jar包
            CompanyDao companyDao = new CompanyImpl();
            companyModel = companyDao.getCompanyModel();
//            jedis.set("companyModel", companyModel.toString());
            jedis.set("logo", companyModel.getLogo());
//            jedis.expire("logo", 1000);
            jedis.set("phone", companyModel.getPhone());
            jedis.set("net", companyModel.getNet());
            jedis.set("email", companyModel.getEmail());
            jedis.set("address", companyModel.getAddress());
            System.out.println("mysql" + a);

        }
        return companyModel;
    }

    public String add(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        return companyDao.add(companyModel) > 0 ? "success" : "error";
    }

    /**
     * 只要管理员删除，更新，增加了数据，都应该去删除key值
     *
     * @param companyModel
     * @return
     */
    public String update(CompanyModel companyModel) {
        CompanyDao companyDao = new CompanyImpl();
        int result = companyDao.update(companyModel);
        if (result > 0) {
            Jedis jedis = new Jedis();
            jedis.del("logo");
        }
        return result > 0 ? "success" : "error";
    }

}
